package org.pp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.pp.core.Assertion;
import org.pp.core.BizExcept;
import org.pp.core.MiniappSessionUtil;
import org.pp.entity.PpUserAddress;
import org.pp.mapper.PpUserAddressMapper;
import org.pp.miniapp.dto.UserAddressAdd;
import org.pp.miniapp.dto.UserAddressEdite;
import org.pp.service.IPpUserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户地址 服务实现类
 * </p>
 *
 * @author haima
 * @since 2024-10-20
 */
@Service
public class PpUserAddressServiceImpl extends ServiceImpl<PpUserAddressMapper, PpUserAddress> implements IPpUserAddressService {

    @Override
    public PpUserAddress defaultAddress() {
        Long userId = MiniappSessionUtil.getUserId();
        LambdaQueryWrapper<PpUserAddress> lambdaQueryWrapper= new LambdaQueryWrapper<PpUserAddress>()
                .eq(PpUserAddress::getIfDefault,true)
                .eq(PpUserAddress::getUserId,userId);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    @Transactional
    public Long add(UserAddressAdd dto) {
        dto.setUserId(MiniappSessionUtil.getUserId());
        if(dto.getIfDefault()){
            //update(new LambdaQueryWrapper<PpUserAddress>())
            LambdaUpdateWrapper<PpUserAddress> qr=
                    new LambdaUpdateWrapper<PpUserAddress>()
                            .set(PpUserAddress::getIfDefault,false)
                            .eq(PpUserAddress::getUserId,MiniappSessionUtil.getUserId());
            update(qr);

        }
        PpUserAddress ppUserAddress = BeanUtil.copyProperties(dto, PpUserAddress.class);
        save(ppUserAddress);
        return ppUserAddress.getAddressId();
    }

    @Override
    @Transactional
    public void edite(UserAddressEdite dto) {
        PpUserAddress address = getById(dto.getAddressId());
        Assertion.nullThrow(address,"地址不存在");
        if(!Objects.equals(MiniappSessionUtil.getUserId(),address.getUserId())){
            throw BizExcept.erro("地址不存在");
        }

        if(dto.getIfDefault()){
            //update(new LambdaQueryWrapper<PpUserAddress>())
            LambdaUpdateWrapper<PpUserAddress> qr=
                    new LambdaUpdateWrapper<PpUserAddress>()
                            .set(PpUserAddress::getIfDefault,false)
                            .eq(PpUserAddress::getUserId,MiniappSessionUtil.getUserId());
            update(qr);
        }
        PpUserAddress ppUserAddress = BeanUtil.copyProperties(dto, PpUserAddress.class);
        updateById(ppUserAddress);
    }

    @Override
    public void delete(String ids) {
        //判定一下
        List<String> list = Arrays.asList(ids.split(","));
        List<PpUserAddress> addressList = list(new LambdaQueryWrapper<PpUserAddress>()
                .in(PpUserAddress::getAddressId, list));

        if(CollUtil.isNotEmpty(addressList)){
            addressList.forEach(e->{
                if(!Objects.equals(e.getUserId(),MiniappSessionUtil.getUserId())){
                    throw BizExcept.erro("地址不存在");
                }
            });
            removeBatchByIds(addressList);
        }
    }
}
